import {Notify, Toast} from "vant";
import router from '@/router';

const _g = {

  showMsg(type = "primary", msg = "消息提示") {
    Notify({
      showClose: true,
      message: msg,
      type: type
    });
  },
  showToast(type, msg) {
    return Toast.loading({
      type: type,
      duration: 0, // 持续展示 toast
      forbidClick: true,
      overlay: true,
      loadingType: 'spinner',
      message: msg
    });
  },
  reLogin() {
    let failLogin = _g.showToast('fail', '请重新登录');
    localStorage.clear();
    setTimeout(() => {
      failLogin.clear();
      router.replace('/login');
    }, 1000);
  },
  dataURLtoFileFun(dataurl, filename) {
    // 将base64转换为文件，dataurl为base64字符串，filename为文件名（必须带后缀名，如.jpg,.png）
    let arr = dataurl.split(",");
    let mime = arr[0].match(/:(.*?);/)[1];
    let bstr = atob(arr[1]);
    let n = bstr.length;
    let u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, {type: mime});
  },
  toFormData(data) {
    let form = new FormData()
    let keys = Object.keys(data)
    keys.forEach(key => {
      form.append(key, data[key])
    })
    return form;
  },
  //传入字符串格式为 "2018-9-30 4:20:00"
  timeAgo(dateTimeString) {
    let dateTimeStamp = Date.parse(dateTimeString.replace(/-/g, '/'));


    let minute = 1000 * 60,
      hour = minute * 60,
      day = hour * 24,
      week = day * 7,
      halfamonth = day * 15,
      month = day * 30,
      now = new Date().getTime(),
      diffValue = now - dateTimeStamp;//时间差

    if (diffValue < 0) {
      return;
    }
    let minC = diffValue / minute;  //计算时间差的分，时，天，周，月
    let hourC = diffValue / hour;
    let dayC = diffValue / day;
    let weekC = diffValue / week;
    let monthC = diffValue / month;
    let result;
    if (monthC >= 1 && monthC <= 6) {
      result = " " + parseInt(monthC) + "月前"
    } else if (weekC >= 1 && weekC <= 3) {
      result = " " + parseInt(weekC) + "周前"
    } else if (dayC >= 1 && dayC <= 6) {
      result = " " + parseInt(dayC) + "天前"
    } else if (hourC >= 1 && hourC <= 23) {
      result = " " + parseInt(hourC) + "小时前"
    } else if (minC >= 1 && minC <= 59) {
      result = " " + parseInt(minC) + "分钟前"
    } else if (diffValue >= 0 && diffValue <= minute) {
      result = "刚刚"
    } else {
      let datetime = new Date();
      datetime.setTime(dateTimeStamp);
      let Nyear = datetime.getFullYear();
      let Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
      let Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
      let Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
      let Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
      let Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
      result = Nyear + "-" + Nmonth + "-" + Ndate
    }
    return result;
  },
  newRandom() {
    return Math.round(Math.random()*100);
  }
}

export default _g;


